package fishBids.group3.utils;

import fishBids.group3.common.Constants;
import jade.content.Concept;
import jade.content.ContentElement;
import jade.content.lang.Codec;
import jade.content.lang.Codec.CodecException;
import jade.content.onto.Ontology;
import jade.content.onto.OntologyException;
import jade.content.onto.UngroundedException;
import jade.content.onto.basic.Action;
import jade.core.AID;
import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;
import jade.util.leap.Collection;
import jade.util.leap.Iterator;
import jade.util.leap.Serializable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class Utils {
	
	private static final String GROUP_NUM = "3";

	public static void registerInDF(String name, String type, Agent agent ){
		
//		System.out.println("Utils: register in df >> Registering name: " + name + ", type: " + type +
//				", agent " + agent);
		// Creates its own description

		DFAgentDescription dfd = new DFAgentDescription();
		ServiceDescription sd = new ServiceDescription();
		sd.setName(name);
		sd.setType(type);
		dfd.addServices(sd);

		// Registers its description in the DF
		try {
			DFService.register(agent, dfd);
		} catch (FIPAException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		System.out.println(type + " " + GROUP_NUM + ": registered in the DF");
		dfd = null;
		sd = null;
	}
	
	public static void sendMessage(int performative, Codec codec, Ontology ontology, AID receiverId, String protocol,
			Agent senderAgent) {   
		
		ACLMessage msg = createMessage(performative, codec, ontology,receiverId, protocol );

		senderAgent.send(msg);

		
	}	
	
	private static ACLMessage createMessage(int performative, Codec codec,
			Ontology ontology, AID receiverId, String protocol) {
		ACLMessage msg = new ACLMessage(performative);

		msg.setLanguage(codec.getName());
		msg.setOntology(ontology.getName());
		msg.addReceiver(receiverId);
		msg.setProtocol(protocol);
		
		return msg;
	}

	public static void sendMessageWithConcept(int performative, Codec codec, Ontology ontology, AID receiverId, String protocol,
			Agent senderAgent, Concept concept ) {   
		
		ACLMessage msg = createMessage(performative, codec,ontology,receiverId, protocol);
		
		try {
			Action agAction = new Action(senderAgent.getAID(),concept);
			senderAgent.getContentManager().fillContent(msg, agAction);
			senderAgent.send(msg);
		} catch (CodecException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OntologyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}	
	
	public static void sendGenericMessageWithObject(int performative, AID receiverId, String protocol,
			Agent senderAgent, Serializable serializable ) {   
		
		ACLMessage msg = new ACLMessage(performative);

		msg.addReceiver(receiverId);
		msg.setProtocol(protocol);
		
		try {
			msg.setContentObject(serializable);
			senderAgent.send(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}	
	
	public static AID VerifyBidder (AID agentID, AID[] bidders ){

		int i=0;
		
	    for (i = 0; bidders[i]!= null; i++){
	            if (agentID == bidders[i])
	                    return agentID;
	    }
	    return null;
	}
	
	public static String getProperty(String key){
		if( in == null ){
			try {
				in = new FileInputStream(f);
				pro.load(in);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		return pro.getProperty(key);
	}
	
	
	static Properties pro = new Properties();
	static File f = new File(Constants.PROPERTIES_FILE);
    static FileInputStream in = null;

	public static void sendMessageWithConceptAgentsCollection(int performative,
			Codec codec, Ontology ontology, Collection biddersReceivers,
			String protocol, Agent senderAgent,
			Concept concept) {
		
		Iterator it = biddersReceivers.iterator();
		
		while (it.hasNext()) {
			AID receiverAID = (AID) it.next();
			if(concept != null ){
				Utils.sendMessageWithConcept(performative,
						codec, 
						ontology, 
						receiverAID,
						protocol, 
						senderAgent,
						concept);				
			}else{
				Utils.sendMessage(performative,
						codec, 
						ontology, 
						receiverAID,
						protocol, 
						senderAgent);
			}
		}
		
		
	}

	public static Concept extractContent(Agent agent,
			ACLMessage msg) {
		
		ContentElement ce;
		try {
			ce = agent.getContentManager().extractContent(msg);
			if (ce instanceof Action) {
				Action ac = (Action) ce;
				return ac.getAction();
			}
		} catch (UngroundedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CodecException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OntologyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;


	}

	public static float calculatePriceDelta(double decreaseLowerLimit,
			double decreaseUpperLimit, float amount) {
		
		return (float) ((decreaseUpperLimit - decreaseLowerLimit) * Math.random() + decreaseLowerLimit);
	}

}
